package com.example.administrator.android2019.ch2Activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.administrator.android2019.R;

import java.text.DecimalFormat;


/**
 * 1、关联布局文件
 * 2、关联控件
 * 3、按钮点击事件
 *    按钮加监听（绑定）
 * */
public class weightActivity extends AppCompatActivity {
   private EditText ed_weight,ed_height;
   private Button btn_computer,btn_about,btn_exit;
   private TextView tv_result;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weight);

        init();

        View.OnClickListener click=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.ch2_bmi_btn_computer:
                        computer();

                        break;
                    case R.id.ch2_bmi_btn_about:
                        about();
                        break;
                    case  R.id.ch2_bmi_btn_exit:
                        exit();
                        break;

                }

            }
        };

        btn_computer.setOnClickListener(click);
        btn_about.setOnClickListener(click);
        btn_exit.setOnClickListener(click);
    }
 /**
  *    退出
  * */
    private void exit() {
        DialogInterface.OnClickListener listener=new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                weightActivity.this.finish();
            }
        };


        AlertDialog.Builder dialog=new AlertDialog.Builder(weightActivity.this);
        dialog.setTitle("退出？");
        dialog.setMessage("真的要退出系统吗？");
        dialog.setPositiveButton("确定",listener);
        dialog.setNegativeButton("取消",null);


        dialog.show();
    }
/**
 *   关于
 *   跳转到另一个Activity
 *
 *
 * */
    private void about() {
        /*从当前Activity跳转到about_Activity */
        Intent intent=new Intent(weightActivity.this,activity_about2Activity.class);
        /*传递数据*/
        intent.putExtra("hxy","sssss");
        startActivity(intent);
    }

    private void computer() {
        /*先获取输入框的内容，再根据公式计算BMI的值，根据BMI的判断过轻或正常等*/
        String s_heinght=ed_height.getText().toString();
        String s_weight= ed_weight.getText().toString();
        Double height=Double.parseDouble(s_heinght);
        height=height/100;
        double weight=Double.parseDouble(s_weight);
        double result=weight/height/height;
        /*结果保留2位小数*/
        DecimalFormat f1=new DecimalFormat("0.00");
        String s="您的体重指数为："+f1.format(result)+"\n 理想指数为20-22\n";
        if (result<18.5){
            s+="过轻";
        }else if (result<23){
            s+="正常";
        }else if (result<25){
            s+="过重";
        }else if (result<30){
            s+="肥胖";
        }else {
            s+="痴肥";
        }
        tv_result.setText(s);

    }

    public void init(){
        ed_height=findViewById(R.id.ch2_bmi_ed_height);
        ed_weight=findViewById(R.id.ch2_bmi_ed_weight);
        btn_computer=findViewById(R.id.ch2_bmi_btn_computer);
        btn_about=findViewById(R.id.ch2_bmi_btn_about);
        btn_exit=findViewById(R.id.ch2_bmi_btn_exit);
        tv_result=findViewById(R.id.ch2_bmi_tv_result);


    }

}
